package persistence;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

import model.TipoMidia;

public class TipoMidiaDaoImpl implements TipoMidiaDao {
	Connection c;

	public TipoMidiaDaoImpl() {
		GenericDao gDao = new GenericDao();
		c = gDao.getConnection();
	}

	@Override
	public void salvaTipoMidia(TipoMidia tipoMidia) throws SQLException {
		String sql = "INSERT INTO tiposDeMidias VALUES (?,?)";
		PreparedStatement ps = c.prepareStatement(sql);

		ps.setInt(1, tipoMidia.getIdTpoMidia());
		ps.setString(2, tipoMidia.getDenominacao());

		ps.execute();
		ps.close();

	}

	@Override
	public void editaTipoMidia(TipoMidia tipoMidia) throws SQLException {
		String sql = "UPDATE tiposDeMidias SET denominacao = ? WHERE idTpoMidia = ?";
		PreparedStatement ps = c.prepareStatement(sql);
		ps.setString(1, tipoMidia.getDenominacao());
		ps.setInt(2, tipoMidia.getIdTpoMidia());
		ps.execute();
		ps.close();

	}

	@Override
	public void excluiTipoMidia(TipoMidia tipoMidia) throws SQLException {
		String sql = "DELETE tiposDeMidias WHERE idTpoMidia = ?";
		PreparedStatement ps = c.prepareStatement(sql);
		ps.setInt(1, tipoMidia.getIdTpoMidia());
		ps.execute();
		ps.close();

	}

	@Override
	public TipoMidia pesquisaTipoMidia(TipoMidia tipoMidia) throws SQLException {
		TipoMidia tm = new TipoMidia();
		String sql = "SELECT idTpoMidia, denominacao FROM tiposDeMidias WHERE idTpoMidia = ?";
		PreparedStatement ps = c.prepareStatement(sql);
		ps.setInt(1, tipoMidia.getIdTpoMidia());
		ResultSet rs = ps.executeQuery();
		if (rs.next()) {
			tm.setIdTpoMidia(rs.getInt("idTpoMidia"));
			tm.setDenominacao(rs.getString("denominacao"));

		}
		ps.close();
		rs.close();
		return tm;
	}

}
